package com.tengju.bff.interfaces.manage.star;

import com.tengju.bff.interfaces.manage.star.facade.StarManageFacade;
import com.tengju.bff.interfaces.manage.star.vo.ListStarDetailVO;
import com.tengju.bff.interfaces.shared.ApiResponse;
import com.tengju.bff.interfaces.shared.servlet.ManageEntrance;
import com.tengju.bff.interfaces.shared.servlet.ManageUtil;
import com.tengju.user.application.archives.StarManageService;
import com.tengju.user.application.archives.dto.BatchIdCodesDTO;
import com.tengju.user.application.archives.dto.BatchStarUpdateDTO;
import com.tengju.user.application.archives.dto.StarListQueryDTO;
import com.tengju.user.domain.model.user.ManageUser;
import com.tengju.user.domain.model.user.Staff;
import com.tengju.user.domain.model.user.StarLevelEnum;
import com.tengju.user.domain.model.user.StarRoleEnum;
import com.tengju.user.domain.shared.ResultCodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@Validated
@RestController
@Api(tags = "星级管理操作")
@RequestMapping(value = "manage/star/opr")
@Slf4j
@ManageEntrance
public class StarManageController {

    @Autowired
    StarManageService starManageService;

    @Autowired
    StarManageFacade starManageFacade;

    @RequiresRoles("sso")
    @ApiOperation(value = "DT后台星级数据查询列表", notes = "dt后台星级数据查询列表")
    @RequestMapping(value = "getStarList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<ListStarDetailVO> getStarAuditList(@Valid @RequestBody StarListQueryDTO starListQueryDTO, BindingResult bindingResult) throws Exception {
        ListStarDetailVO listStarDetailVO = starManageFacade.getListStarDetail(starListQueryDTO);
        return ApiResponse.newSuccess(listStarDetailVO);
    }

    @RequiresRoles("sso")
    @ApiOperation(value = "DT后台批量修改星级", notes = "dt后台批量修改星级")
    @RequestMapping(value = "batchUpdateStar", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<Boolean> batchUpdateStar(@Valid @RequestBody BatchStarUpdateDTO batchStarUpdateDTO, BindingResult bindingResult) throws Exception {
        Staff staff = ManageUtil.getOperateUser();
        Integer targetStarLevel = batchStarUpdateDTO.getTargetStarLevel();
        StarRoleEnum targetStarRole = StarLevelEnum.getStarRoleCodeByLevel(targetStarLevel);
        if (targetStarRole == null) {
            return ApiResponse.newFailure(ResultCodeEnum.STAR_INVALID);
        }
        batchStarUpdateDTO.setTargetStarRole(targetStarRole.getCode());
        starManageService.batchUpdateStar(batchStarUpdateDTO, staff);
        return ApiResponse.newSuccess(true);
    }

    @RequiresRoles("sso")
    @ApiOperation(value = "腾炬后台星级身份作废", notes = "腾炬后台星级身份作废")
    @RequestMapping(value = "batchInvalid", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<Boolean> batchInvalid(@Valid @RequestBody BatchIdCodesDTO batchIdCodesDTO, BindingResult bindingResult) throws Exception {
        Staff staff = ManageUtil.getOperateUser();
        starManageService.batchInvalid(batchIdCodesDTO.getIdCodes(), staff);
        return ApiResponse.newSuccess(true);
    }

}
